﻿/***************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@gmail.com)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace BaoJianSoft.Web.Extensions.DynamicPages
{
	/// <summary>
	/// Class to configure gridview field.
	/// </summary>
	public sealed class GridViewFieldConfiguration
	{
		private string headerText;
		private string sortingFieldName;

		/// <summary>
		/// Construct Basic Data Field instance.
		/// </summary>
		/// <param name="fieldName"></param>
		/// <param name="headerText"></param>
		public GridViewFieldConfiguration(string fieldName, string headerText)
		{
			this.FieldName = fieldName;
			this.HeaderText = headerText;
			this.Sortable = true;
		}

		/// <summary>
		/// Get field name
		/// </summary>
		public string FieldName { get; private set; }

		/// <summary>
		/// Get sorting field name. If property value is null or empty, returns FieldName instead.
		/// </summary>
		public string SortingFieldName
		{
			set { this.sortingFieldName = value; }
			get
			{
				if (!string.IsNullOrEmpty(this.sortingFieldName))
					return this.sortingFieldName;

				return this.FieldName;
			}
		}

		/// <summary>
		/// Set/get output of cell
		/// </summary>
		public string Output { get; set; }

		/// <summary>
		/// Get field label
		/// </summary>
		public string HeaderText
		{
			private set { this.headerText = value; }
			get { return WebUtility.FormatDisplayText(this.headerText); }
		}

		/// <summary>
		/// Set/get value to format display field value.
		/// </summary>
		public string DisplayFormat { get; set; }

		/// <summary>
		/// Set/get conditional display formatter.
		/// </summary>
		public ConditionalDisplayFormatter ConditionalDisplayFormatter { get; set; }

		/// <summary>
		/// Set/get true when the field is sortable. The default value is TRUE.
		/// </summary>
		public bool Sortable { get; set; }

		/// <summary>
		/// Set/get binding field type.
		/// </summary>
		public GridViewFieldTypes FieldType { get; set; }

		/// <summary>
		/// Set/get field header displaying style value
		/// </summary>
		public TableItemStyle HeaderStyle { get; set; }

		/// <summary>
		/// Set/get field item displaying style value
		/// </summary>
		public TableItemStyle ItemStyle { get; set; }
	}

	/// <summary>
	/// Conditional display formatter.
	/// </summary>
	public class ConditionalDisplayFormatter
	{
		/// <summary>
		/// Enumerable conditional display format matchers.
		/// </summary>
		public IEnumerable<ConditionalDisplayFormatMatcher> Matchers { get; set; }

		/// <summary>
		/// Callback to format display field value.
		/// </summary>
		public IConditionalDisplayFormatCallback Callback { get; set; }
	}

	public class ConditionalDisplayFormatMatcher
	{
		private string formatString;

		/// <summary>
		/// Get field value to match
		/// </summary>
		public string ValueToMatch { get; private set; }

		/// <summary>
		/// Get string to format matched value.
		/// </summary>
		public string FormatString 
		{
			get { return WebUtility.FormatDisplayText(this.formatString); }
			private set { this.formatString = value; }
		}

		/// <summary>
		/// Construct ConditionalDisplayFormatMatcher instance
		/// </summary>
		/// <param name="valueToMatch"></param>
		/// <param name="formatString"></param>
		public ConditionalDisplayFormatMatcher(string valueToMatch, string formatString)
		{
			this.ValueToMatch = valueToMatch;
			this.FormatString = formatString;
		}
	}

	/// <summary>
	/// Interface to conditionally format display field value.
	/// </summary>
	public interface IConditionalDisplayFormatCallback
	{
		/// <summary>
		/// Format input field value.
		/// </summary>
		/// <param name="fieldValue"></param>
		/// <returns></returns>
		string Format(string fieldValue);
	}

	/// <summary>
	/// Binding field type
	/// </summary>
	public enum GridViewFieldTypes
	{
		Text, 
		UserLink,
		OrganizationLink
	}
}

